<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/12/26
 * Time: 13:23
 */
namespace app\admin\controller;
use app\common\lib\db\DbCommon;
use app\common\lib\exception\ApiException;
use think\Db;
use think\Request;
class Question extends Base{
    public function mode()
    {
        if (request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //用户模糊昵称
            if(isset($keyword) && $keyword!="")
            {
                $map['name'] = ['like',"%" . $keyword . "%"];
            }
            try{
                $count = Db::name('question_type')->where($map)->count();  //总条目数据
                $lists = Db::name('question_type')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function mode_add()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $is_array = check_array($data);
            if(!$is_array) {
                throw new ApiException('非法请求','500');
            }
            $data['set_time'] = date('Y-m-d H:i:s');
            try{
                $res = Model('QuestionType')->save($data);
            }catch (\Exception $e){
                throw new ApiException($e->getMessage(),'500');
            }
            if(!$res) {
                throw new ApiException('数据库错误','500');
            }
            return show('200','添加成功',$res);
        }
        return $this->fetch();
    }

    public function mode_del()
    {
        $type_id = Request::instance()->param('id');
        try {
            $res = Model('QuestionType')->where(['type_id' => $type_id])->delete();
        }catch (\Exception $e) {
            return json(['code'=>200,'msg'=>$e->getMessage()]);
        }
        if(!$res) {
            return json(['code'=>500,'msg'=>'数据库错误']);
        }
        Model('Question')->where(['type' => $type_id])->delete();
        return json(['code'=>200,'msg'=>'已删除']);
    }

    public function question_list()
    {
        if (request()->isAjax()) {
            extract(input()); //将接收的参数赋值给变量
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $map = array();
            //用户模糊昵称
            if(isset($keyword) && $keyword!="")
            {
                $map['title'] = ['like',"%" . $keyword . "%"];
            }
            try{
                $count = Db::name('question')->where($map)->count();  //总条目数据
                $lists = Db::name('question')->page($NowPage,$limits)->where($map)->select();  //当前页
            }catch (\Exception $e) {
                return json(['code' => 500, 'msg' => $e->getMessage()]);
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    public function question_add()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $is_array = check_array($data);
            if(!$is_array) {
                throw new ApiException('非法请求','500');
            }
            $msg = array();
            $msg['title'] = $data['data']['title'];
            $msg['type'] = $data['data']['type'];
            $msg['content'] = $data['content'];
            $msg['set_time'] = date('Y-m-d H:i:s');
            try{
                $ret = Model('Question')->save($msg);
            }catch (\Exception $e) {
                throw new ApiException($e->getMessage());
            }
            if(!$ret) {
                throw new ApiException('数据库错误','200');
            }
            return show('200','发布成功',$msg);
        }
        $mode_list = Model('QuestionType')->select();
        $this->assign([
            'mode_list' => $mode_list
        ]);
        return $this->fetch();
    }

    public function question_edit()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $msg = array();
            $msg['title'] = $data['data']['title'];
            $msg['type'] = $data['data']['type'];
            $msg['content'] = $data['content'];
            $msg['set_time'] = date('Y-m-d H:i:s');
//            return show('200','ok',$msg);
            $res = Model('Question')->update_question_msg($data['data']['id'],$msg);
            if(!$res) {
                throw new ApiException('没有做任何更改','200');
            }
            return show('200','已更新',$res);
        }else {
            $id = Request::instance()->param('id');
            $data = Db::name('question')->where(['id' => $id])->find();
            $mode_list = Model('QuestionType')->select();
            $this->assign([
                'data' => $data,
                'mode_list' => $mode_list
            ]);
            return $this->fetch();
        }
    }

    public function update()
    {
        if(\request()->isPOST()){
            $data = input('post.');
            $res = Model('UUser')->update_user_msg($data['user_id'],$data);
            if(!$res) {
                throw new ApiException('没有做任何更改','200');
            }
            return show('200','已更新',$res);
        }else {
            $user_id = Request::instance()->param('user_id');
            $data = Db::name('u_user')->where(['user_id' => $user_id])->find();
            $area = DbCommon::area($data['user_sheng'],$data['user_shi'],$data['user_qu']);
            $this->assign([
                'data' => $data,
                'area' => $area
            ]);
            return $this->fetch();
        }
    }

    /**
     * Ajax抛送删除
     * @return \think\response\Json
     */
    public function question_del()
    {
        $id = Request::instance()->param('id');
        $res = Model('Question')->where(['id'=>$id])->delete();
        if(!$res) {
            return json(['code'=>500,'msg'=>'数据库错误']);
        }
        return json(['code'=>200,'msg'=>'已删除']);
    }

    /**
     * 查看是否已经删除，而不是删除继续点击删除
     * @param number $user_id 用户id
     * @return bool $is_del
     */
    public function is_del($user_id)
    {
        $res = Model('UUser')->get(['user_id' => $user_id,'user_status' => '4']);
        if(!$res) {
            return false;
        }
        return true;
    }
}